package com.transaction.manage.util;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

@Component
public class SpringUtil implements ApplicationContextAware {

    private static ApplicationContext context;

    private SpringUtil(){}

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        SpringUtil.context = applicationContext;
    }

    public static <T> T getBean(Class<T> clazz){
        if(clazz == null)return null;
        return context.getBean(clazz);
    }

    public static <T> T getBean(String beanName){
        if(StringUtils.isEmpty(beanName))return null;
        return (T)context.getBean(beanName);
    }

    public static <T> T getBean(String beanName,Class<T> clazz){
        if(StringUtils.isEmpty(beanName) || clazz == null) return null;
        return (T)context.getBean(beanName,clazz);
    }
    public static ApplicationContext getContext(){
        if(context == null) return null;
        return context;
    }



}
